<template>
  <div class="home-wrap">
    <div class="right_input">
      <div>
        <div class="login-text">
          登录
        </div>
        <el-form ref="formRef" label-position="left" :model="form" :rules="rules" @submit.prevent>
          <el-form-item prop="account">
            <el-input v-model.trim="form.account" placeholder="请输入账号" type="text">
              <template #prefix>
                <el-icon class="el-input__icon">
                  <User />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="password" style="margin-top: 20px;">
            <el-input v-model.trim="form.password" placeholder="请输入密码" type="text" show-password>
              <template #prefix>
                <el-icon class="el-input__icon">
                  <Lock />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>
        </el-form>
        <div class="el-button-style">
          <el-button type="primary" @click="goLogin" :loading="loading">
            登录
          </el-button>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { Lock, User } from '@element-plus/icons-vue'
import { ref, reactive, onMounted } from 'vue'
  //@ts-ignore
import { useUserStore } from '@/stores/modules/user'
  //@ts-ignore
import { useSettingsStore } from '@/stores/modules/settings'
import { useRouter } from 'vue-router' // 引用vue-router

const formRef = ref<any>(null)
const form = reactive({
  account: '',
  password: '',
})
const rules = reactive<any>({
  account: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
})
const loading = ref(false)

const settingsStore = useSettingsStore()
const userStore = useUserStore()
const router = useRouter()
const login = (form: any) => userStore.login(form)

const goLogin = () => {
  if (formRef.value)
    formRef.value.validate(async (valid: any) => {
      if (valid)
        try {
          loading.value = true
          const loginFlag = await login(form).catch(() => {
            loading.value = false
          })

          if (loginFlag) {
            const result = settingsStore.getRouter
            if (result[0]?.path) {
              router.push(`${result[0].path}`)
            } else {
              router.push('/home')
            }
          }

        } finally {
          loading.value = false
        }
    })
}


onMounted(()=>{
  const { token } = userStore
  if (token)  router.push('/home')
})


</script>

<style lang="scss" scoped>
.home-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  background: url('@/assets/loag/login_bg.jpg') no-repeat center;
  background-size: cover;

  .right_input {
    position: absolute;
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
    right: 0;
    padding: 100px 50px 0;
    width: 350px;
    height: 100%;
    background-color: #fffc;

    .login-text {
      margin-bottom: 50px;
      font-size: 34px;
      font-weight: 600;
    }
  }

  .el-button-style {

    height: 35px;
    margin-top: 20px;

    :deep(.el-button) {
      width: 100%;
    }
  }
}
</style>